package cn.yinyu.queue.module.ecg.controller.admin.devmanage;

import cn.yinyu.queue.module.ecg.controller.admin.jobrecord.vo.JobRecordPageReqVO;
import cn.yinyu.queue.module.ecg.controller.admin.jobrecord.vo.JobRecordStatisticVO;
import cn.yinyu.queue.module.ecg.dal.dataobject.devmanage.DeviceStatisticDO;
import cn.yinyu.queue.module.ecg.dal.dataobject.jobrecord.JobRecordStatisticDO;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.time.LocalDate;
import java.util.*;
import java.io.IOException;

import cn.yinyu.queue.framework.common.pojo.PageParam;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.pojo.CommonResult;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;
import static cn.yinyu.queue.framework.common.pojo.CommonResult.success;

import cn.yinyu.queue.framework.excel.core.util.ExcelUtils;

import cn.yinyu.queue.framework.apilog.core.annotation.ApiAccessLog;
import static cn.yinyu.queue.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.yinyu.queue.module.ecg.controller.admin.devmanage.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.devmanage.DeviceDO;
import cn.yinyu.queue.module.ecg.service.devmanage.DeviceService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 设备")
@RestController
@RequestMapping("/ecg/device")
@Validated
public class DeviceController {

    @Resource
    private DeviceService deviceService;

    @PostMapping("/create")
    @Operation(summary = "创建设备")
    @PreAuthorize("@ss.hasPermission('ecg:device:create')")
    public CommonResult<Integer> createDevice(@Valid @RequestBody DeviceSaveReqVO createReqVO) {
        return success(deviceService.createDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备")
    @PreAuthorize("@ss.hasPermission('ecg:device:update')")
    public CommonResult<Boolean> updateDevice(@Valid @RequestBody DeviceSaveReqVO updateReqVO) {
        deviceService.updateDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ecg:device:delete')")
    public CommonResult<Boolean> deleteDevice(@RequestParam("id") Integer id) {
        deviceService.deleteDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ecg:device:query')")
    public CommonResult<DeviceRespVO> getDevice(@RequestParam("id") Integer id) {
        DeviceDO device = deviceService.getDevice(id);
        return success(BeanUtils.toBean(device, DeviceRespVO.class));
    }

    @GetMapping("/get-by-dev-id")
    @Operation(summary = "获得设备")
    @Parameter(name = "devId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ecg:doctor:task')")
    public CommonResult<DeviceRespVO> getDevice(@RequestParam("dev-id") String devId) {
        DeviceDO device = deviceService.getDevice(devId);
        return success(BeanUtils.toBean(device, DeviceRespVO.class));
    }

    @PostMapping("/update-dev-state")
    @Operation(summary = "更新设备状态")
    @Parameter(name = "devId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ecg:doctor:task')")
    public CommonResult<Integer> updateDeviceState(@Valid @RequestBody DeviceSaveReqVO updateReqVO) {
        updateReqVO.setStateDate( LocalDate.now() );
        Integer ret =  deviceService.updateDeviceState(updateReqVO);
        return success(ret);
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备分页")
    @PreAuthorize("@ss.hasPermission('ecg:device:query')")
    public CommonResult<PageResult<DeviceRespVO>> getDevicePage(@Valid DevicePageReqVO pageReqVO) {
        PageResult<DeviceDO> pageResult = deviceService.getDevicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备 Excel")
    @PreAuthorize("@ss.hasPermission('ecg:device:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceExcel(@Valid DevicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceDO> list = deviceService.getDevicePage(pageReqVO).getList();

        List<DeviceRespFlattenVO> listFlatten = list.stream().map(deviceDO -> {
            DeviceRespFlattenVO deviceRespFlattenVO = BeanUtils.toBean(deviceDO, DeviceRespFlattenVO.class);
            if (null != deviceDO.getPatDetails()) {
                deviceRespFlattenVO.setPatId(deviceDO.getPatDetails().getId());
                deviceRespFlattenVO.setPatName(deviceDO.getPatDetails().getName());
                deviceRespFlattenVO.setPatMobile(deviceDO.getPatDetails().getMobile());
                deviceRespFlattenVO.setPatDeptCode(deviceDO.getPatDetails().getDeptCode());
                deviceRespFlattenVO.setPatDeptDesc(deviceDO.getPatDetails().getDeptDesc());
                deviceRespFlattenVO.setPatWardCode(deviceDO.getPatDetails().getWardCode());
                deviceRespFlattenVO.setPatWardDesc(deviceDO.getPatDetails().getWardDesc());
                deviceRespFlattenVO.setPatBedNo(deviceDO.getPatDetails().getBedNo());
            }
            return deviceRespFlattenVO;
        }).toList();

        // 导出 Excel
        ExcelUtils.write(response, "设备.xls", "数据", DeviceRespFlattenVO.class, listFlatten);
    }

    @GetMapping("/dev-statistic")
    @Operation(summary = "设备统计")
    @PreAuthorize("@ss.hasPermission('ecg:device:statistic')")
    public CommonResult<List<DeviceStatisticVO>> getDeviceStatistic(@Valid DevicePageReqVO pageReqVO) {
        List<DeviceStatisticDO> statisticDOList = deviceService.getDeviceStatistic(pageReqVO);
        return success(BeanUtils.toBean(statisticDOList, DeviceStatisticVO.class));
    }

}
